<?php

namespace fast;

use think\Db;

use think\Exception;
use fast\Http;
/**
 * Http 请求类
 */
class Yongyou
{

  
       
    public static function getToken()
    {
    
        $app = Db::table('fa_yongyou')->find(1);

        $time = time();

        if ($app['expiresIn'] > time()) {
            return $app;
        }


        $url = 'https://openapi.chanjet.com/v1/common/auth/selfBuiltApp/generateToken';

        $data['appTicket'] = $app['appTicket'];
        $data['certificate'] = $app['certificate'];
     
        $sysParams = json_encode($data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

        $headers = ['Content-type: application/json','appKey:'.$app['appKey'],'appSecret:'.$app['appSecret']];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        $response = Http::sendRequest($url, $sysParams, 'POST', $options);
        
        $result = json_decode($response['msg'],true);
        
       

        if ($result['result']) {
           
            $expiresIn = $result['value']['expiresIn']+$time;
            $refreshExpiresIn = $result['value']['refreshExpiresIn']+$time;
            $map['id'] = 1;
            $updata['accessToken'] = $result['value']['accessToken'];
            $updata['refreshToken'] = $result['value']['refreshToken'];
            $updata['expiresIn'] = $expiresIn;
            $updata['refreshExpiresIn'] = $refreshExpiresIn;
            Db::table('fa_yongyou')->where($map)->update($updata);
            $app = Db::table('fa_yongyou')->find(1);

        }

        
        return $app;

    }
    





     public static function addOrder($id)
    {       
    
        $app = self::getToken();

      

        $url = 'https://openapi.chanjet.com/accounting/openapi/cc/salesorder/add/3070349950845428';

        $data['code'] = 'QF'.date('YmdHis');
        $data['bizDate'] = date('Y-m-d');
        $data['custVendorCode'] = 'RETAIL_CUSTOMER';
        $data['goodsItem'] = [];
        $data['goodsItem'][0]['productCode'] = '11010001';//商品编码
        $data['goodsItem'][0]['warehouseCode'] = '11010001';//仓库编码
        $data['goodsItem'][0]['quantity'] = '10'; //数量
        $data['goodsItem'][0]['unit'] = '瓶'; //计量单位
        $data['goodsItem'][0]['price'] = '9.90'; //单价
        $data['goodsItem'][0]['amount'] = '99'; //金额
        
     
        $sysParams = json_encode($data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

        $headers = ['Content-type: application/json','appKey:'.$app['appKey'],'appSecret:'.$app['appSecret'],'openToken:'.$app['accessToken']];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];


       
        $response = Http::sendRequest($url, $sysParams, 'POST', $options);
        
        $result = json_decode($response['msg'],true);
        
       

        // if ($result['result']) {
           
        //     $expiresIn = $result['value']['expiresIn']+$time;
        //     $refreshExpiresIn = $result['value']['refreshExpiresIn']+$time;
        //     $map['id'] = 1;
        //     $updata['accessToken'] = $result['value']['accessToken'];
        //     $updata['refreshToken'] = $result['value']['refreshToken'];
        //     $updata['expiresIn'] = $expiresIn;
        //     $updata['refreshExpiresIn'] = $refreshExpiresIn;
        //     Db::table('fa_yongyou')->where($map)->update($updata);
        //     $app = Db::table('fa_yongyou')->find(1);

        // }

        
        return $app;

    }
    


      public static function addOrder2($id)
    {       




        $order = Db::table('fa_pifa_order')->find($id);


        if (empty($order)) {
            logs('order_tongbu','订单ID----'.$id.'-----订单不存在');     
            return;
        }



        $mendian = Db::table('fa_pifa_shangjia')->find($order['mendian_id']);



        if (empty($mendian)) {
           logs('order_tongbu','订单ID----'.$id.'-----门店不存在');     
           return;
        }
    
        $app = self::getToken();


        $url = 'https://openapi.chanjet.com/accounting/openapi/cc/salesorder/add/3070349950845428';

        $data['code'] = $order['order_no'];
        $data['bizDate'] = $order['createday'];
        $data['custVendorCode'] = 'RETAIL_CUSTOMER';
        $data['warehouseCode'] = $mendian['memberId'];

        $map2['order_id'] = $id;
        $OrderitemModel = new \app\admin\model\pifa\Orderitem;

        $order_item = $OrderitemModel::with(['goodsitem'])->where($map2)->select();



        if (empty($order_item)) {
           logs('order_tongbu','订单ID----'.$id.'-----商品没查到');     
           return;
        }
        $data['goodsItem'] = [];



        foreach ($order_item as $key => $value) {
            $data['goodsItem'][$key]['productCode'] = $value['goodsitem']['goods_no'];//商品编码
            $data['goodsItem'][$key]['warehouseCode'] = $mendian['memberId'];//仓库编码
            $data['goodsItem'][$key]['quantity'] = strval($value['goods_num']); //数量 
            $data['goodsItem'][$key]['price'] = $value['goodsitem']['goods_price']; //单价
            $data['goodsItem'][$key]['unit'] = $value['goodsitem']['danwei']; //计量单位
            $data['goodsItem'][$key]['amount'] = $value['price']; //金额

        }

        var_dump($data);
        return;
     
        $sysParams = json_encode($data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
          
        $headers = ['Content-type: application/json','appKey:'.$app['appKey'],'appSecret:'.$app['appSecret'],'openToken:'.$app['accessToken']];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        $response = Http::sendRequest($url, $sysParams, 'POST', $options);
 
        $result = json_decode($response['msg'],true);
        
 
        
        $map_order['id'] = $id;    
        if (isset($result['success'])) {

            if ($result['success']) {
                $updata['shanghu_status'] = '1';
                Db::table('fa_pifa_order')->where($map_order)->update($updata);
            }else{
                logs('order_tongbu','订单请求数据----'.$sysParams);
                logs('order_tongbu','订单返回失败----'.json_encode($result,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
                $updata['shanghu_msg'] = $result['message'];
                Db::table('fa_pifa_order')->where($map_order)->update($updata);
            }
            
        }else{
            logs('order_tongbu','订单请求数据----'.$sysParams);
            logs('order_tongbu','订单返回失败----'.json_encode($result,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
            $updata['shanghu_msg'] = '请求失败';
            Db::table('fa_pifa_order')->where($map_order)->update($updata);
        }

        
        return true;

    }
    


}
